package com.ysd.controller;

import java.util.HashMap;

import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.dao.rolesDao;
import com.ysd.entity.Roles;
import com.ysd.entity.User;
import com.ysd.service.rolesService;
/**
 * 角色测试
 * @author CJP
 *
 */
@RestController
public class rolesController {
	//测试建表
	@Autowired
	rolesDao rd;
	@Autowired
	rolesService rs;
	@RequestMapping("/getByRid")
	public Object getByRid(Integer id) {
		System.out.println("成功建表");
		return rd.getByRid(id);
	}
	//查询和分页
	@RequestMapping("/getAl")
	public Map<String, Object> getA(Integer page, Integer limit,String rname,Integer rid) {
		//分頁查詢
		Pageable p = new PageRequest(page-1, limit);
			Page<Roles> findAll = rd.findAll(p);
			List<Roles> data=findAll.getContent();
			long count = findAll.getTotalElements();
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("code", 0);
			map.put("msg", "");
			map.put("data", data);
			map.put("count", count);
		return map;
	}
	
	//查询角色
	@GetMapping("/getall1")
	public   List<Roles>  finda(){
	List<Roles> r=rs.findAllRoles();
		return r;
	}
	/**
	 * 根据用户id查看用户角色
	 */
	@GetMapping("/getuseridfindRoles")
	public List<Roles> findusers(Integer uid) {
		System.out.println("根据用户id查看用户角色"+uid);
		List<Roles> u = rs.findAll(uid);
		return u;
	}
	//向用户添加角色
	@PostMapping("/AddUserToRole")
	public Integer AddUserToRole(Integer uid,Integer rid){
		System.out.println(uid+"----------------------------");
		System.out.println(rid+"----------------------------");
		Integer a =rs.saveUserrole(uid, rid);
		return a;
		
	}
	//用户删除角色
	@RequestMapping("/delUserToRole")
	public void delUserToRole(Integer uid,Integer rid){
		System.out.println(uid+"----------------------------");
		System.out.println(rid+"----------------------------");
		rs.delUserrole(uid, rid);
	}
	
}
